// 过滤url参数
const getUrlParam = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.href.split('?')[1] && window.location.href.split('?')[1].match(reg); //匹配目标参数
	if (r != null) return unescape(r[2]);
	return null; //返回参数值
}

// 时间过滤器
const filterData = function(timestamp, num) {
	// num = 0 返回YY-MM-DD
	// num != 0 返回YY-MM-DD hh-mm-ss
	timestamp = timestamp + '';
	timestamp = timestamp.length == 10 ? timestamp * 1000 : timestamp;
	timestamp = Number(timestamp)
	var date = new Date(timestamp);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	var minute = date.getMinutes();
	var second = date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

// 时间过滤器去掉时分秒
const filterData2 = function(timestamp, num) {
	// num = 0 返回YY-MM-DD
	// num != 0 返回YY-MM-DD hh-mm-ss
	var timestamp = timestamp +''
	timestamp = timestamp.length == 10 ? timestamp * 1000 : timestamp;
	var date = new Date(Number(timestamp));
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	return y + '-' + m + '-' + d;
}

// 年龄  周岁
const age = function (idCard,birthday) {
	var age = ''
	if(idCard){	
		var myDate = new Date();
		var month = myDate.getMonth() + 1;
		var day = myDate.getDate();
		age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
		if (idCard.substring(10, 12) < month || idCard.substring(10, 12) == month && idCard.substring(12, 14) <= day) {
			age++;
		}
	}else{
		var myDate = new Date();
		var month = myDate.getMonth() + 1;
		var day = myDate.getDate();
		age = myDate.getFullYear() - Number(birthday.substring(0, 4)) - 1;
		if(birthday.substring(4,2) == month || birthday.substring(4, 2) == month && birthday.substring(8,10) <= day) {
			age++
		}
	}
	return age
}

//卷去头部的距离
const distance = function (classn) {
	let callback = false
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	var offsetTop = classn ? document.querySelector('.'+classn).offsetTop : '';
	if(offsetTop || offsetTop != null) {
		if (scrollTop > offsetTop) {
		  callback = true
		} else {
		  callback = false
		}
		return callback
	}
}

//判断是否是在微信浏览器中打开
const weixin = function (){
	let ua = navigator.userAgent.toLowerCase();
	return ua.indexOf('micromessenger') != -1;
}

//正则验证（限制输入）
const regular = function(e,item,v) {
	if(v == 'oneDecimal') {
		// 保留一位小数
		var reg = /^[0-9]+(\.[0-9]{1})?$/;
		if(item.name.indexOf('体重指数') > -1) {
			if(e.detail.value > 27.9 || e.detail.value < 0 ){
				uni.showToast({
					title:'请输入0-27.9区间（保留一位小数）',
					icon:'none'
				})
				item.envalue = ''
				return	
			}else{
				if(isNaN(Number(e.detail.value)) == true) {
					uni.showToast({
						title:'请输入0-27.9区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else {
					if(e.detail.value) {
						item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
					}else {
						item.envalue = ''
					}
				}
			}
		}else if(item.name.indexOf('空腹血糖') > -1) {
			if(!reg.test(e.detail.value)){
				if(e.detail.value > 6.9 || e.detail.value < 0 ){
					uni.showToast({
						title:'请输入0-6.9区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else{
					if(isNaN(Number(e.detail.value)) == true) {
						uni.showToast({
							title:'请输入0-6.9区间（保留一位小数）',
							icon:'none'
						})
						item.envalue = ''
						return	
					}else {
						if(e.detail.value) {
							item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
						}else {
							item.envalue = ''
						}
					}
				}
			}else{
				if(e.detail.value > 6.9 || e.detail.value < 0 ){
					uni.showToast({
						title:'请输入0-6.9区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else{
					if(isNaN(Number(e.detail.value)) == true) {
						uni.showToast({
							title:'请输入0-6.9区间（保留一位小数）',
							icon:'none'
						})
						item.envalue = ''
						return	
					}else {
						if(e.detail.value) {
							item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
						}else {
							item.envalue = ''
						}
					}
					
				}
			}
		}else if(item.name.indexOf('餐后1小时血葡萄糖') > -1) {
			if(!reg.test(e.detail.value)){
				if(e.detail.value > 11.1 || e.detail.value < 0 ){
					uni.showToast({
						title:'请输入0-11.1区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else{
					if(isNaN(Number(e.detail.value)) == true) {
						uni.showToast({
							title:'请输入0-11.1区间（保留一位小数）',
							icon:'none'
						})
						item.envalue = ''
						return	
					}else {
						if(e.detail.value) {
							item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
						}else {
							item.envalue = ''
						}
					}
				}
			}else{
				if(e.detail.value > 11.1 || e.detail.value < 0 ){
					uni.showToast({
						title:'请输入0-11.1区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else{
					if(isNaN(Number(e.detail.value)) == true) {
						uni.showToast({
							title:'请输入0-11.1区间（保留一位小数）',
							icon:'none'
						})
						item.envalue = ''
						return	
					}else {
						if(e.detail.value) {
							item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
						}else {
							item.envalue = ''
						}
					}
					
				}
			}			
		}else if(item.name.indexOf('高密度脂蛋白') > -1) {
			if(!reg.test(e.detail.value)){
				if(e.detail.value > 300 || e.detail.value < 0 ){
					uni.showToast({
						title:'请输入0-300区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else{
					if(isNaN(Number(e.detail.value)) == true) {
						uni.showToast({
							title:'请输入0-300区间（保留一位小数）',
							icon:'none'
						})
						item.envalue = ''
						return	
					}else {
						if(e.detail.value) {
							item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
						}else {
							item.envalue = ''
						}
					}
				}
			}else{
				if(e.detail.value > 300 || e.detail.value <= 0 ){
					uni.showToast({
						title:'请输入0-300区间（保留一位小数）',
						icon:'none'
					})
					item.envalue = ''
					return	
				}else{
					if(isNaN(Number(e.detail.value)) == true) {
						uni.showToast({
							title:'请输入0-300区间（保留一位小数）',
							icon:'none'
						})
						item.envalue = ''
						return	
					}else {
						if(e.detail.value) {
							item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(1) : Number(e.detail.value+'');
						}else {
							item.envalue = ''
						}
					}
					
				}
			}
		}	
	}else if(v == 'integer') {
		var reg =  /^\d+$/;
		if(item.name.indexOf('运动') > -1 || item.name.indexOf('喝几次酒') > -1) {
			if(!reg.test(e.detail.value)){
				uni.showToast({
					title:'请输入0-7的正整数',
					icon:'none'
				})
				item.envalue =  ''
				return				
			}else{
				if(e.detail.value > 7 || e.detail.value < 0){
					item.envalue =  ''
					uni.showToast({
						title:'请输入0-7的正整数',
						icon:'none'
					})
					return
				}else{
					item.envalue = Number(e.detail.value+'')
				}
			}
		}else if(item.name.indexOf('脂肪类') > -1 || item.name.indexOf('含糖食品') > -1 || item.name.indexOf('低纤维素食品') > -1) {
			if(!reg.test(e.detail.value)){
				uni.showToast({
					title:'请输入0-2000的正整数',
					icon:'none'
				})
				item.envalue =  ''
				return				
			}else{
				if(e.detail.value >= 2000 || e.detail.value < 0){
					item.envalue =  ''
					uni.showToast({
						title:'请输入0-2000的正整数',
						icon:'none'
					})
					return
				}else{
					item.envalue = Number(e.detail.value+'')
				}
			}
		}else if(item.name.indexOf('喝水') > -1 || item.name.indexOf('喝多少酒') > -1) {
			if(!reg.test(e.detail.value)){
				uni.showToast({
					title:'请输入0-5000的正整数',
					icon:'none'
				})
				item.envalue =  ''
				return				
			}else{
				if(e.detail.value > 5000 || e.detail.value < 0){
					item.envalue =  ''
					uni.showToast({
						title:'请输入0-5000的正整数',
						icon:'none'
					})
					return
				}else{
					item.envalue = Number(e.detail.value+'')
				}
			}
		}else if(item.name.indexOf('年龄') > -1 || item.name.indexOf('腰围') > -1 || item.name.indexOf('收缩压') > -1) {
			if(!reg.test(e.detail.value)){
				uni.showToast({
					title:'请输入0-300的正整数',
					icon:'none'
				})
				item.envalue =  ''
				return				
			}else{
				if(e.detail.value > 300 || e.detail.value < 0){
					item.envalue =  ''
					uni.showToast({
						title:'请输入0-300的正整数',
						icon:'none'
					})
					return
				}else{
					item.envalue = Number(e.detail.value+'')
				}
			}
		}
	}else if(v == 'twoDecimal') {
		var reg = /^[0-9]+(\.[0-9]{2})?$/;
			// 0到300保留两位小数
			if(!reg.test(e.detail.value)){
				if(e.detail.value > 300  || e.detail.value < 0){
					item.envalue =  ''
					uni.showToast({
						title:'请输入0-300的区间（保留两位小数）',
						icon:'none'
					})
					return
				}else{
					if(e.detail.value) {
						item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(2) : Number(e.detail.value+'');
					}else {
						item.envalue = ''
					}
				}
			}else{
				if(e.detail.value > 300  || e.detail.value < 0){
					item.envalue =  ''
					uni.showToast({
						title:'请输入0-300的区间（保留两位小数）',
						icon:'none'
					})
					return
				}else{
					if(e.detail.value) {
						item.envalue = (e.detail.value + '').indexOf('.') !== -1 ?  Number(e.detail.value).toFixed(2) : Number(e.detail.value+'');
					}else {
						item.envalue = ''
					}
				}
			}
	}
}

export {
	getUrlParam,
	filterData,
	filterData2,
	age,
	distance,
	weixin,
	regular
}
